﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Facade;
using IronOne.SalesOne.BusinessObjects;
using System.Configuration;

namespace IronOne.SalesOne.SalesOneWeb.UserManagement
{
    public partial class DeviceMapping : SOSecurityPage
    {
        StructureFacade facade;
        int userType;
        private int pageNo = 1;
        int pageSize = Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"].ToString());
        bool IsLastPage;

        protected void Page_Load(object sender, EventArgs e)
        {
            facade = new StructureFacade();

            if (!Page.IsPostBack)
            {
                pageNo = 1;
                ValidateUserCredentials(Request.Url.LocalPath);

                User user = facade.GetUser(User.Identity.Name);
                //User user = facade.GetUser("ss");
                UserInformation info = facade.GetUserInformation(User.Identity.Name);
                userType = info.UserType;

                bool flag = false;

                if (userType == 4)
                {
                    /*if (user.SoCode != null)
                    {
                        ddlSO.Items.Add(user.SoCode);
                        ddlSO.SelectedIndex = ddlSO.Items.Count - 1;

                        ddlSO.Enabled = false;
                    }
                    else*/
                    populateSOCodes(facade.GetSOsInUnit(user.NodeId));
                    flag = true;
                }

                if (flag == true)
                {
                    ddlUnit.Items.Add(info.UnitName);
                    ddlUnit.SelectedIndex = ddlUnit.Items.Count - 1;

                    ddlUnit.Enabled = false;
                }
                else if (userType == 3)
                {
                    populateUnits(facade.GetUnitsForBranch(user.NodeId));
                    flag = true;
                }

                if (flag == true)
                {
                    ddlBranch.Items.Add(info.BranchName);
                    ddlBranch.SelectedIndex = ddlBranch.Items.Count - 1;

                    ddlBranch.Enabled = false;
                }
                else if (userType == 2)
                {
                    populateBranches(facade.GetBranchesForGroup(user.NodeId));
                    flag = true;
                }

                if (userType == 1)
                    populateBranches(facade.GetBranches("BranchName ASC"));

                //populateBranches(facade.GetBranches("BranchName ASC"));                

                setControls();
                txtPageNo.Text = pageNo.ToString();
            }
        }


        private void setControls()
        {

            if (ddlBranch.SelectedIndex <= 0)
                btnSearch.Enabled = false;
        }

        private void populateBranches(List<Branch> branches)
        {
            ddlBranch.DataSource = branches;
            ddlBranch.DataTextField = "BranchName";
            ddlBranch.DataValueField = "BranchID";
            ddlBranch.DataBind();

            addDefaultItem(ddlBranch);
        }

        private void populateUnits(List<SalesOneUnit> units)
        {
            ddlUnit.DataSource = units;
            ddlUnit.DataTextField = "Name";
            ddlUnit.DataValueField = "UnitID";
            ddlUnit.DataBind();

            addDefaultItem(ddlUnit);
        }



        private void populateSOCodes(List<string> users)
        {
            ddlSO.Items.Clear();
            foreach (string str in users)
            {
                ddlSO.Items.Add(str);
            }
            addDefaultItem(ddlSO);
        }

        private void addDefaultItem(DropDownList ddl)
        {
            ListItem newItem = new ListItem("Select...");
            ddl.Items.Insert(0, newItem);
        }

        protected void ddlBranch_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlBranch.SelectedIndex > 0)
            {
                populateUnits(facade.GetUnitsInBranch(int.Parse(ddlBranch.SelectedValue)));
                ddlUnit.SelectedIndex = 0;
                ddlSO.SelectedIndex = 0;
            }
            setControls();
        }

        protected void ddlUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlUnit.SelectedIndex > 0)
            {
                populateSOCodes(facade.GetSOsInUnit(int.Parse(ddlUnit.SelectedValue)));
            }
            setControls();
        }

        /*protected void ddlSO_SelectedIndexChanged(object sender, EventArgs e)
        {
            gridDataBind();
        }*/

        private void gridDataBind(int pageNo)
        {
            int? unit = null;
            if (ddlUnit.SelectedIndex > 0)
                unit = int.Parse(ddlUnit.SelectedValue);

            List<MappingInfo> list = facade.GetMappingInfo(int.Parse(ddlBranch.SelectedValue),
                                                                unit,
                                                                ddlSO.SelectedIndex > 0 ? ddlSO.SelectedValue : null,
                                                                pageNo);

            if (list.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
            {
                IsLastPage = (facade.GetMappingInfo(int.Parse(ddlBranch.SelectedValue),
                                                                unit,
                                                                ddlSO.SelectedIndex > 0 ? ddlSO.SelectedValue : null,
                                                                pageNo + 1)).Count > 0 ? false : true;
            }
            GridDeviceMapping.DataSource = list.Count > 0 ? list : null;
            GridDeviceMapping.DataBind();

            if (list.Count <= 0)
            {
                MassageSummery("IIS504");
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            pageNo = 1;
            txtPageNo.Text = "1";
            gridDataBind(pageNo);
        }

        protected void cbStatus_CheckedChanged(object sender, EventArgs e)
        {
            MappingInfo info = new MappingInfo();
            GridViewRow row = (GridViewRow)((CheckBox)sender).Parent.Parent;

            info.SOCode = row.Cells[1].Text;
            //info.DeviceId = row.Cells[0].Text;
            //info.SyncDate = DateTime.Parse(row.Cells[2].Text);
            info.SyncStatus  = ((CheckBox)sender).Checked;

            if (facade.UpdateMappingInfo(info))
            {
                MassageSummery("IIS401");
            }
            else
            {
                MassageSummery("IIS502");
            }
        }

        protected void GridDeviceMapping_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                MappingInfo item = (MappingInfo)e.Row.DataItem;
                GridViewRow info = (GridViewRow)e.Row;
                ((CheckBox)info.Cells[3].FindControl("cbStatus")).Checked = item.SyncStatus;
            }
        }

        protected void lbtNext_Click(object sender, EventArgs e)
        {
            if (txtPageNo.Text != "1")
            {
                pageNo = Convert.ToInt32(txtPageNo.Text) + 1;
                txtPageNo.Text = pageNo.ToString();

            }
            else
            {
                pageNo = 2;
                txtPageNo.Text = "2";

            }
            
            gridDataBind(pageNo);
        }

        protected void lbtPrev_Click(object sender, EventArgs e)
        {
            if (txtPageNo.Text != "1")
            {
                pageNo = Convert.ToInt32(txtPageNo.Text) - 1;
                txtPageNo.Text = pageNo.ToString();

                gridDataBind(pageNo);
            }
        }

        protected void lbtGo_Click(object sender, EventArgs e)
        {
            int p = int.Parse(txtPageNo.Text);
            if (p != 0)
            {
                try
                {
                    gridDataBind(p);
                    pageNo = p;
                }
                catch (Exception)
                {

                    throw;
                }

            }
        }

        protected void GridDeviceMapping_DataBound(object sender, EventArgs e)
        {
            int rw = GridDeviceMapping.Rows.Count;

            if (txtPageNo.Text != "1")
            {
                lbtPrev.Visible = true;
            }

            if (rw > 0)
            {                
                int curr = 0;

                if (!string.IsNullOrEmpty(txtPageNo.Text))
                {
                    curr = Convert.ToInt32(txtPageNo.Text);
                }
                else
                {
                    curr = 1;
                }


                if (curr == 1)
                {

                    if (IsLastPage)
                    {
                        lbtNext.Visible = false;
                        lbtGo.Visible = false;
                        txtPageNo.Visible = false;
                        lbtPrev.Visible = false;
                    }
                    else
                    {
                        lbtNext.Visible = true;
                        lbtGo.Visible = true;
                        txtPageNo.Visible = true;
                        lbtPrev.Visible = false;
                    }



                }
                else if (IsLastPage)
                {
                    lbtNext.Visible = false;
                    lbtGo.Visible = true;
                    txtPageNo.Visible = true;
                    lbtPrev.Visible = true;
                }
                else
                {
                    lbtNext.Visible = true;
                    lbtGo.Visible = true;
                    txtPageNo.Visible = true;
                    lbtPrev.Visible = true;
                }


               
            }
            else
            {
                lbtNext.Visible = false;
                lbtPrev.Visible = false;
                lbtGo.Visible = false;
                txtPageNo.Visible = false;
            }

        }

    }
}  

